<?php 
include_once('connection/connection.php');
//session_start();
//ob_start();
include('header.php');
include('menu.php');
$habit_count=$_SESSION['habit_count'];
//$form_id=$_SESSION['formID'];
$form_id=trim($_REQUEST['formId']);
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link href="css/style_question.css" type="text/css" rel="stylesheet" />
<script language="javascript" type="text/javascript" src="js/validate2.js"></script>
<script language=javascript>
function show_critical(val,i)
{ 
		if(val == "yes" )
		{
			document.getElementById('crit'+i).style.display = "block";	
		}
		else
		{
			document.getElementById('crit'+i).style.display = "none";
			
			var a=document.getElementsByName('CH_quality'+i+'[]')
			a[0].checked=false;
			a[1].checked=false;
			a[2].checked=false;
			a[3].checked=false;
			a[4].checked=false;
			var b=document.getElementsByName('CH_quantity'+i+'[]')
			b[0].checked=false;
			b[1].checked=false;
			b[2].checked=false;
			b[3].checked=false;
			b[4].checked=false;
		} 
}
function show_tanker(val)
{
//alert(val);
		if(val == "yes" )
		{
			document.getElementById('tanker').style.display = "block";	
		}
		else
		{
			//document.getElementById('tanker').style.display = "none";
			var num = document.getElementById('habcount').value;
			document.getElementById('tanker').style.display = "none";
			for(i=1; i<=num; i++)
			{
			document.getElementById('water_supply_no'+i).value="";
			document.getElementById('water_supply_qty'+i).value="";
			document.getElementById('water_supply_area'+i).value="";
			}
		}
}


function onload_critical()
{ 
		var counthab=document.getElementById('habcount').value;
		for(var i=1; i<counthab; i++)
		{
		var arr=document.getElementsByName('critical'+i);
		if(arr[0].checked)
		{
			document.getElementById('crit'+i).style.display = "block";	
		}
		else
		{
			document.getElementById('crit'+i).style.display = "none";
		}
		} 
}
</script>
<title>Questionnaire No.2</title>
</head>
<body onLoad="onload_critical();">
<?php
if(isset($_POST['submit']))
{
	$fclick = true;
	$q2_id=$form_id;
	//$q2_id=$_SESSION['q2_id'];
	//$habit_count=$_SESSION['habit_count'];
	$sqlhab="select * from tbl_dtls_q2_habit where q2_id=$q2_id";
	$queryhab=mysql_query($sqlhab);
	$hab_count=mysql_num_rows($queryhab);
	for($i=1; $i<=$hab_count; $i++)
	{
		$critical_hname = 'critical_hname'.$i;
		$critical_hcode = 'critical_hcode'.$i;
		$type1_habit = 'type1_habit'.$i;
		$critical = 'critical'.$i;
		$CH_quality='CH_quality'.$i;
		$CH_quantity='CH_quantity'.$i;
		
		$critical_hname=$_POST[$critical_hname];
		$critical_hcode=$_POST[$critical_hcode];
		$type1_habit=$_POST[$type1_habit];
		$critical=$_POST[$critical];
		$CH_quality=$_POST[$CH_quality];
		if(count($CH_quality)!=0)
		{	
			$CH_quality=implode(',',$CH_quality);
		}
		else
		{
			$CH_quality='';
		}
		
		$CH_quantity=$_POST[$CH_quantity];	
		if(count($CH_quantity)!=0)
		{	
			$CH_quantity=implode(',',$CH_quantity);
		}
		else
		{
			$CH_quantity='';
		}
		
		//$qr2="INSERT INTO tbl_dtls_q2_critical(q2_id, critical_hname, critical_hcode, type_habit, critical, CH_quality, CH_quantity) VALUES ('$q2_id', '$critical_hname', '$critical_hcode', '$type1_habit', '$critical', '$CH_quality', '$CH_quantity')";
		//echo $qr2;
		$qr2="UPDATE tbl_dtls_q2_critical SET critical='$critical', CH_quality='$CH_quality', CH_quantity='$CH_quantity' WHERE q2_id=$form_id and critical_hcode=$critical_hcode";
		$rec2=mysql_query($qr2);
		
		
		$water_supply_hcode = 'water_supply_hcode'.$i;
		$water_supply_hname = 'water_supply_hname'.$i;
		$type2_habit = 'type2_habit'.$i;
		$water_supply_no = 'water_supply_no'.$i;
		$water_supply_qty = 'water_supply_qty'.$i;
		$water_supply_area = 'water_supply_area'.$i;
		$water_supply_hcode=$_POST[$water_supply_hcode];	
		$water_supply_hname=$_POST[$water_supply_hname];
		$type2_habit=$_POST[$type2_habit];
		$water_supply_no=$_POST[$water_supply_no];
		$water_supply_qty=$_POST[$water_supply_qty];
		$water_supply_area=$_POST[$water_supply_area];

		//$qr3="INSERT INTO tbl_dtls_q2_tanker(q2_id,habitation_code, habitation_name, type_habit, total_tanker_no, water_quantity, area_covered) value ('$q2_id', '$water_supply_hcode', '$water_supply_hname', '$type2_habit', '$water_supply_no', '$water_supply_qty', '$water_supply_area')";
		//echo $qr3;
		$qr3="UPDATE tbl_dtls_q2_tanker SET total_tanker_no='$water_supply_no', water_quantity='$water_supply_qty', area_covered='$water_supply_area' WHERE q2_id=$form_id and habitation_code=$water_supply_hcode";
		$rec3=mysql_query($qr3);
	}
	
			if($rec2)
			{
				//header('Location: questionnaire3_2.php');
				//echo "success 1";	
				$relocatePage=true;	
			}
			else 
			{
				$errorIns="Error Updating tbl_dtls_q1_critical table <br>";
			}
			if($rec3)
			{
				
				//header('Location: questionnaire3_2.php');
				//echo "success 1";	
				$relocatePage=true;	
			}
			else 
			{
				$errorIns="Error Updating tbl_dtls_q1_tanker table <br>";
			}
	
		$q="SELECT usertype FROM tbl_dtls_createuser WHERE loginid='$loginid'";
			//echo $q;
			
			$res=mysql_query($q);
			$x=mysql_fetch_array($res);
			$user_type=$x['usertype'];
			$user_id=$_SESSION['loginid'];
			$formId=$_SESSION['form_id'];
			$user_name=$_SESSION['name'];
			//$user_type=$_SESSION['usertype'];
			$ip=$_SERVER['REMOTE_ADDR'];
			$qr1="INSERT INTO tbl_dtls_edit_history(user_id, form_id, action_id, ip_address, modify_date, user_type, user_name) VALUES ('$user_id', '$formId', '5', '$ip', NOW(), '$user_type', '$user_name')";
			//echo $q5;
			$res=mysql_query($qr1);
			if($res)
			{
				//header('Location: questionnaire3_2.php');
				//echo "success 1";	
				$relocatePage=true;	
			}
			else 
			{
				$errorIns="Error inserting tbl_dtls_edit_history table <br>";
			}	
			if($errorIns==0) 
			{
				echo'<span class="successmssg"><b>Congratulation!!</b><br/>You have successfully updated this Page. Please go to <a href="dd_questionnaire_page.php">Edit Questionnaire Page Wise</a></span>';
				exit();
			}
			else 
			{
				echo $errorIns;exit;
			}		
		
}
else
{
	$fclick = false;
	$flag=0;    
}
?>
<div align="center">
<form name="f1" action="" method="post" onSubmit="return validate4()">
	<table border="0" width="750" id="table1" cellspacing="0" cellpadding="0">
		<tr>
			<td height="30" colspan="2">
			<p align="center">
			<font face="Verdana" style="font-size: 10pt; font-weight: 700" color="#000000">
			Questionnaire No. </font><span style="font-weight: 700">
			<font face="Verdana" size="2" color="#000000">2</font></span></td>
		</tr>
		<tr>
			<td height="19" colspan="2">
			<p align="center">
			<font face="Arial" style="font-size: 8.5pt" color="#000000">(For 
			Hand Pump Schemes only)</font></td>
		</tr>
		<tr>
			<td height="18" colspan="2">
				<p align="center">
				<font style="font-size: 8.5pt" face="Arial" color="#000000">Page 4</font></p>			</td>
		</tr>
		<tr><td colspan="3">&nbsp;</td></tr>
		<tr>
			<td height="22" colspan="2" bgcolor="#EEEEEE"><font color="#000000">&nbsp;
			<span style="font-weight: 700">
			<font face="Arial" style="font-size: 8.5pt">Critical Habitations</font></span>
			</font></td>
		</tr>
		<tr>
			<td colspan="2">
				<table border="0" width="750" id="table2" bgcolor="#FDF9EE" cellspacing="1" cellpadding="0">
					<tr>
						<td width="226" height="28" align="center"  bgcolor="#F7E6B7">
							<font face="Verdana" style="font-size: 8.5pt" color="#990000">
							Habitation Name</font></td>
							<td width="121" align="center"  bgcolor="#F7E6B7"><font face="Verdana" style="font-size: 8.5pt" color="#990000">Habitation Type</font></td>
						<td width="399"  bgcolor="#F7E6B7" align="center">
							<font face="Verdana" style="font-size: 8.5pt" color="#990000">
							Critical</font></td>
					</tr>
					<?php
				/*$hname=$_SESSION['arr_habit_name'];
				$hcode=$_SESSION['arr_habit_code'];
				$i=1;
				foreach($hname as $key=>$val)
				{*/
				
					$sql_crit="select * from tbl_dtls_q2_critical where q2_id=$form_id order by critical_hcode";
					$query_crit=mysql_query($sql_crit);
					//$row_crit=mysql_fetch_array($query_crit);
					//echo $row_crit['critical_hname'];
 	 			$i=1;
				while($row_crit=mysql_fetch_array($query_crit)){
				//echo $row_crit['critical_hname'];
				?>
					
				<tr align="center">
					<td width="226">
					<input type="text" name="critical_hname<?php echo $i;?>" id="critical_hname<?php echo $i;?>" value="<?php echo $row_crit['critical_hname'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px">
	                <input type="hidden" name="critical_hcode<?php echo $i;?>" id="critical_hcode<?php echo $i;?>" value="<?php echo $row_crit['critical_hcode'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px">				  </td>
				  <td align="center" width="121"><input type="text" name="type1_habit<?php echo $i;?>" id="type1_habit<?php echo $i;?>" value="<?php echo $row_crit['type_habit'];?>" size="2" class="txt_1" readonly="readonly"/></td>
				  <td width="399">
				  <font style="font-size: 9pt" face="Verdana" color="#000000">
				  <input type="radio" name="critical<?php echo $i;?>" id="critical<?php echo $i;?>" value="yes"<?php echo($row_crit['critical']=='yes')?'checked':'';?> onClick="show_critical(this.value,<?php echo $i; ?>)">Yes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	  		  	  <input type="radio" name="critical<?php echo $i;?>" id="critical<?php echo $i;?>" value="no"<?php echo($row_crit['critical']=='no')?'checked':'';?> onClick="show_critical(this.value,<?php echo $i; ?>)">No&nbsp;</font></td>
				</tr> 
					<tr>
						<td></td>
						<td></td>
						<td><div id="crit<?php echo $i;?>" class="hideme">
							<table border="0" width="100%" id="table16" cellpadding="0">
							<tr>
								<td height="22" colspan="5">
								<font face="Verdana" style="font-size: 8.5pt" color="#990000">&nbsp; 
								Critical Village in terms of quality </font>							                        		</td>
							</tr>
							<tr>
							<?php $ch_quality=explode(',',$row_crit['CH_quality']);?>
								<td width="86" height="24">&nbsp;
					  	   		<input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="Fluoride"<?php echo  (in_array('Fluoride',$ch_quality)?'checked':'');?>>
						  		<font face= "Verdana" style="font-size: 8.5pt" color="#000000">                           		Fluoride </font> </td>
				    		  <td width="77"><input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="Nitrates"<?php echo  (in_array('Nitrates',$ch_quality)?'checked':'');?>>
			    		      <font face= "Verdana" style="font-size: 8.5pt" color="#000000"> Nitrates</font> </td>
					    		<td width="62">
				 	    		<input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="TDS"<?php echo  (in_array('TDS',$ch_quality)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">TDS</font> </td>
					    		<td width="83">
						  		<input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="Salinity"<?php echo  (in_array('Salinity',$ch_quality)?'checked':'');?>> 
					  	  		<font style="font-size: 8.5pt" face="Verdana" color="#000000">Salinity                          		</font></td>
						      <td width="79"><input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="None"<?php echo  (in_array('None',$ch_quality)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">None</font></td>
							</tr>
							<tr>
								<td height="26" colspan="5">&nbsp;
								<font face="Verdana" style="font-size: 8.5pt" color="#990000">
								Critical Village in terms of quantity </font>            						                       			</td>
							</tr>
							<tr>
							<?php $ch_quantity=explode(',',$row_crit['CH_quantity']);
							
							?>
								<td height="41" colspan="2">
								&nbsp;&nbsp;<input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Non Availability of water"<?php echo  (in_array('Non Availability of water',$ch_quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">&nbsp;Non Availability of &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;water in Village</font>								</td>
					    		<td height="41" colspan="3"><input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Insufficient quantity of water"<?php echo  (in_array('Insufficient quantity of water',$ch_quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000"> Insufficient quantity of water to &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meet minimum demand</font></td>
				    		</tr>
							<tr>
								<td height="40" colspan="2">
								&nbsp;
							  <input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Emergency response system"<?php echo  (in_array('Emergency response system',$ch_quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">&nbsp;Emergency response &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system</font>								</td>
					    		<td height="40" colspan="3"><input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Excess drawal of water"<?php echo  (in_array('Excess drawal of water',$ch_quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000"> Excess drawal of water by &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intermediate village</font></td>
				    		</tr>
							<tr>
								<td height="26" colspan="2">
								&nbsp;&nbsp;<input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="None"<?php echo  (in_array('None',$ch_quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">&nbsp;None</font></td>
				    		</tr>
						</table>
						</div>
					</td>
				</tr>
				<?php  $i++;
					} ?>
			</table>
		</td>
	</tr>
		<tr>
			<td height="19" colspan="2"><span style="font-size: 4pt">&nbsp;</span></td>
		</tr>
		<tr>
			<td width="227" height="22" bgcolor="#EEEEEE"><font color="#000000">&nbsp;
				<span style="font-weight: 700">
				<font face="Arial" style="font-size: 8.5pt">Water Supply by Tankers</font></span></font></td>
			<td width="523" bgcolor="#EEEEEE">
				  <font style="font-size: 9pt" face="Verdana" color="#000000">
				  <input type="radio" name="supply" id="supply" value="yes" onClick="show_tanker(this.value)" checked="checked">Yes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	  	    <input type="radio" name="supply" id="supply" value="no" onClick="show_tanker(this.value)">No&nbsp;</font></td>
		</tr>
		<tr>
			<td height="14" colspan="2"><span style="font-size: 3pt">&nbsp;</span></td>
		</tr>
		<tr>
			<td colspan="2">
				<table border="0" width="100%" id="table6" bgcolor="#FDF9EE" cellspacing="1" cellpadding="0">
					<tr>
					 <td height="64"><table border="0"  width="100%" id="table8" cellpadding="0" bgcolor="#FDF9EE" cellspacing="1">
                        <tr>
                          <td><div id="tanker">
                            <table width="750">
                                <tr>
                                  <td width="224" height="29" align="center"  bgcolor="#F7E6B7"><font face="Verdana" style="font-size: 8.5pt" color="#990000"> Habitation Name</font></td>
                                  <td width="125" align="center"  bgcolor="#F7E6B7"><font face="Verdana" style="font-size: 8.5pt" color="#990000">Habitation Type</font></td>
                                  <td width="125"  bgcolor="#F7E6B7" align="center"><address>
                                    <span style="font-style: normal"> <font face="Verdana" style="font-size: 8.5pt" color="#990000"> Tanker Nos.</font></span>
                                  </address></td>
                                  <td width="99"  bgcolor="#F7E6B7" align="center"><address>
                                    <span style="font-style: normal"> <font face="Verdana" style="font-size: 8.5pt" color="#990000"> Quantity</font></span>
                                  </address></td>
                                  <td width="169"  bgcolor="#F7E6B7" align="center"><address>
                                    <span style="font-style: normal"> <font face="Verdana" style="font-size: 8.5pt" color="#990000"> Area Covered</font></span>
                                  </address></td>
                                </tr>
                                <?php
				/*$hname=$_SESSION['arr_habit_name'];
				$hcode=$_SESSION['arr_habit_code'];
				$i=1;
				foreach($hname as $key=>$val)
				{*/
				$sql_tanker="select * from tbl_dtls_q2_tanker where q2_id=$form_id order by habitation_code";
				$query_tanker=mysql_query($sql_tanker);
				//$row_tanker=mysql_fetch_array($query_tanker);
 	 			$i=1;
				while($row_tanker=mysql_fetch_array($query_tanker)){
					?>
                                <tr align="center">
                                  <td width="224" height="30"><input type="text" name="water_supply_hname<?php echo $i;?>" id="water_supply_hname<?php echo $i;?>2" value="<?php echo $row_tanker['habitation_name'];?>" style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px" readonly="readonly">
                                      <input type="hidden" name="water_supply_hcode<?php echo $i;?>" id="water_supply_hcode<?php echo $i;?>2" value="<?php echo $row_tanker['habitation_code'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px">                                  </td>
                                  <td align="center" width="125"><input type="text" name="type2_habit<?php echo $i;?>" id="type_habit2<?php echo $i;?>" value="<?php echo $row_tanker['type_habit'];?>" size="2" class="txt_1" readonly="readonly"/></td>
                                  <td width="125"><input type="text" name="water_supply_no<?php echo $i;?>" id="water_supply_no<?php echo $i;?>" value="<?php echo $row_tanker['total_tanker_no'];?>" class="txt5" onBlur="numeric(this)">                                  </td>
                                  <td width="99"><input type="text" name="water_supply_qty<?php echo $i;?>" id="water_supply_qty<?php echo $i;?>" value="<?php echo $row_tanker['water_quantity'];?>" class="txt5" onBlur="numeric(this)">                                  </td>
                                  <td width="169"><input type="text" name="water_supply_area<?php echo $i;?>" id=																												"water_supply_area<?php echo $i;?>" value="<?php echo $row_tanker['area_covered'];?>" class="txt5"></td>
                                
                        <?php  $i++;
								} ?>
                        <input type="hidden" name="habcount" id="habcount" value="<?php echo $i; ?>">
                      </tr>
                              </table>
                          </div></td>
                        </tr>
					  </table></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">
			<table border="0" width="100%" id="table4" cellspacing="1" cellpadding="0" bgcolor="#FDF9EE">
			</table></td>
	</tr>
	
	<tr>
		<td colspan="2">
			<p align="center">
			<input type="submit" value="Update" name="submit" style="font-size: 8pt;font-family: Verdana; border-style: solid; border-width: 1px">
		</td>
	</tr>
</table>
</form>
</div>


</body>
</html>